package com.stone.design.mode.pipeline.tomcat;

import com.google.common.collect.Lists;
import com.stone.design.mode.pipeline.tomcat.valve.PipelineValve;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * StandardPipeline
 *
 * @author wenpan 2023/11/17 9:51 下午
 */
@Data
@Slf4j
public class StandardPipeline implements Pipeline {

    private List<PipelineValve> pipelineValveList = Lists.newArrayList();

    @Override
    public boolean invoke(PipelineContext pipelineContext) {
        boolean isResult = true;
        for (PipelineValve pipelineValve : pipelineValveList) {
            isResult = pipelineValve.execute(pipelineContext);
            // TODO 这里如果某个阀门返回false是否要阻断
            if (!isResult) {
                log.error("{},exec is wrong", pipelineValve.getClass().getSimpleName());
            }
        }
        return isResult;
    }

    @Override
    public boolean addValue(PipelineValve pipelineValve) {
        if (pipelineValveList.contains(pipelineValve)) {
            return true;
        }
        return pipelineValveList.add(pipelineValve);
    }

    @Override
    public boolean removeValue(PipelineValve pipelineValve) {
        return pipelineValveList.remove(pipelineValve);
    }
}